package com.cl.utility.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author liddhappy
 * @className CORSConfig
 * @description 跨域配置
 * @date 2019/10/8 10:20
 */
@Configuration
public class CORSConfig {
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        /**
         * 是否允许请求带有验证信息
         */
        corsConfiguration.setAllowCredentials(true);
        /**
         * 允许访问的客户端域名
         */
        corsConfiguration.addAllowedOrigin("*");
        /**
         * 允许服务端访问的客户端请求头
         */
        corsConfiguration.addAllowedHeader("*");
        /**
         * 允许访问的方法名,get post等
         */
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
